#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define	MAX_NUMBER		0xFFFFFFFF
#define	HASH_SHIFT		20
#define	HASH_NUMBERS	MAX_NUMBER>>HASH_SHIFT

typedef struct t_article
{
	int name;
	int count;
} article;

struct t_pair
{
	unsigned int k;
	unsigned int v;
};

struct t_list_item
{
	struct t_list_item *next;
//	struct t_list_item *prev;
//	int idx;
	void *data;
};

typedef struct t_list_item  list_item;
typedef struct t_pair pair;

int list_cmp_pair(void *a, void *b)
{
	return ((pair*)a)->k-((pair*)b)->k;
}

int list_cmp_int(void *a, void *b)
{
	return *(int*)a-*(int*)b;
}

list_item *list_find_item(list_item *head,void *value,int(*cmp)(void *a,void *b))
{
	//printf("find_item v=%d\n",*(int*)value);
	list_item *ptr=head->next;
	while(ptr!=NULL)
	{
		if(cmp(ptr->data,value)==0) return ptr;
		ptr=ptr->next;
	}
//	puts("NIMA");
	return NULL;
}

list_item *list_insert(list_item *head, void *data, int data_size)
{
//	puts("list_insert ENTER");

	list_item *end,*ptr=head;
	end=malloc(sizeof(list_item));
	while(ptr->next!=NULL)ptr=ptr->next;
	ptr->next=end;
//	end->prev=ptr;
	end->next=NULL;
	end->data=data;
//	end->data=malloc(data_size);
//	memcpy(end->data,data,data_size);
//	printf("*data=%d end->data=%d\n",*(int*)data,*(int*)(end->data));
//	puts("list_insert EXIT");
	return end;
}

void list_delete_item(list_item *head, void *item)
{
}

void list_delete_idx(list_item *head, int idx)
{
}

void *list_get_item(list_item *head, int idx)
{
}

int list_get_size(list_item *head)
{
	list_item *ptr=head;
	int ret=0;
	while(head!=NULL)
	{
		head=head->next;
		ret++;
	}
	return ret;
}

list_item *hash_get_article(list_item *hash_art,pair *data)
{
//	printf("finding: %d %d\n",data->k,data->v);
	return list_find_item(hash_art+(data->k>>HASH_SHIFT),data,list_cmp_pair);
}

list_item *hash_insert_article(list_item *hash_art,pair *data)
{
	return list_insert(hash_art+(data->k>>HASH_SHIFT),data,sizeof(pair));
}

int hash_delete_article(list_item *hash_art, int key)
{
}

pair *new_pair(int k,int v)
{
	pair *ret=malloc(sizeof(pair));
	ret->k=k;
	ret->v=v;
	return ret;
}

int *clone_int(int i)
{
	int *ret=malloc(sizeof(unsigned int));
	memcpy(ret,&i,sizeof(unsigned int));
	return ret;
}

void list_item_init(list_item *item)
{
	item->next=NULL;
//	item->prev=NULL;
//	articles.idx=0;
	item->data=NULL;
}

int main()
{
	list_item articles,*ptr=&articles,*hash,*r;
	unsigned int N,i,v,k;
	pair *p;

	list_item_init(&articles);
	hash=malloc(sizeof(list_item)*HASH_NUMBERS);
	
	if(hash==NULL)
		return -1;
		
	for(i=0;i<HASH_NUMBERS;i++)
	{
		list_item_init(hash+i);
	}
	
	scanf("%u",&N);	
	for(i=0;i<N;i++)
	{
//		v=malloc(sizeof(int));
		scanf("%u %u",&k,&v);
//		scanf("%d",&k);
//		printf("r=%d\n",*p);
		p=new_pair(k,v);
		r=hash_get_article(hash,p);
//		r=list_find_item(&articles,clone_int(k),list_cmp_int);
		if(r==NULL)
		{
//			printf("NIE ZNALAZŁEM %d, TRZA DODAĆ !!!\n",k);
			ptr=list_insert(ptr,clone_int(k),sizeof(int));
			hash_insert_article(hash,p);
			//free(p);
		} else
		{
			((pair*)r->data)->v+=v;
//			printf("ZNALAZŁEM DZIADA! k=%d v=%d\n",k,((pair*)r->data)->v);
			free(p);
		}
	
//		ptr=list_insert(ptr,&k,sizeof(int));
//		hash_insert_article(hash,k,clone_int(k),sizeof(int));
		printf("%u\n",i);
	}

	printf("%d\n",list_get_size(&articles)-1);
	
	/*for(i=0;i<N;i++)
	{
		scanf("%d",&k);
		r=list_find_item(&articles,&k,list_cmp_int);
		if(r==NULL)
		{
			printf("DUPA!! W LIŚCIE NIE MA %d\n",k);
		}
		else
		{
			printf("W LIŚCIE Jest %d\n",k);
		}
		r=hash_get_article(hash,k);
		if(r==NULL)
		{
			printf("DUPA!! W HASH NIE MA %d\n",k);
		}
		else
		{
			printf("W HASH Jest %d\n",k);
		}		
	}
	
*/

	ptr=articles.next;
//	printf("Article's list:\n");
	while(ptr!=NULL)
	{
		p=(pair*)ptr->data;
		r=hash_get_article(hash,p);
		//printf("%d %d\t",p->k,p->v);
		if(r==NULL)
		{
			//printf("DUPA!! NIE MA %d\n",p->k);
		}
		else
		{
			//printf("list:\t%d %d\n",((pair*)r->data)->k,((pair*)r->data)->v);
			printf("%u %u\n",((pair*)r->data)->k,((pair*)r->data)->v);
		}
		ptr=ptr->next;
		printf("S");
	}

/*
	scanf("%d",&i);
	ptr=list_find_item(&articles,&i,list_cmp_int);
	if(ptr!=NULL)
		puts("jest");
	else
		puts("nie ma!");
*/	
	return 0;
}
